package jgl.vajra.management.part.mine.activity;

import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.HashMap;
import java.util.Map;

import jgl.vajra.management.R;
import jgl.vajra.management.bean.MemberCenterBean;
import jgl.vajra.management.databinding.ActivityMemberRulesBinding;
import jgl.vajra.management.manager.AccountManager;
import jgl.vajra.management.part.mine.contract.MemberRulesContract;
import jgl.vajra.management.part.mine.viewModel.MemberRulesViewModel;
import jgl.vajra.management.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.management.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.management.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.management.widget.utils.UIUtils;

@CreateViewModel(MemberRulesViewModel.class)
public class MemberRulesActivity extends BaseMVVMActivity<MemberRulesViewModel, ActivityMemberRulesBinding> implements MemberRulesContract.View {

    private SingleTypeBindingAdapter adapter;
    private SingleTypeBindingAdapter adapterRules;

    @Override
    public int getLayoutId() {
        return R.layout.activity_member_rules;
    }

    @Override
    public void initView() {
        super.initView();

        // 不可嵌套滑动
        mBinding.recyclerView.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerView.setFocusableInTouchMode(false);
        //设置布局管理器
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SingleTypeBindingAdapter(this, null, R.layout.item_member_rules);
        mBinding.recyclerView.setAdapter(adapter);

        // 不可嵌套滑动
        mBinding.recyclerViewRules.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerViewRules.setFocusableInTouchMode(false);
        //设置布局管理器
        mBinding.recyclerViewRules.setLayoutManager(new LinearLayoutManager(this));
        adapterRules = new SingleTypeBindingAdapter(this, null, R.layout.item_member_rules_score);
        mBinding.recyclerViewRules.setAdapter(adapterRules);

        requestNetData();
    }

    private void requestNetData() {
        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
        mViewModel.getMemberCenterData(map);
    }


    @Override
    public void returnMemberCenterData(MemberCenterBean data) {
        adapter.refresh(data.contentData);

        for (int i = 0; i < data.ruleData.size(); i++) {
            data.ruleData.get(i).levelText=data.ruleData.get(i).level+"分";
            data.ruleData.get(i).rateText=data.ruleData.get(i).rate+"%";
        }
        data.ruleData.get(data.ruleData.size()-1).levelText=data.ruleData.get(data.ruleData.size()-1).level+"分以下";
        data.ruleData.get(data.ruleData.size()-1).rateText=data.ruleData.get(data.ruleData.size()-1).rate+"";
        MemberCenterBean.RuleBean ruleBean = new MemberCenterBean.RuleBean();
        ruleBean.levelText="评分级别";
        ruleBean.rateText="会员费返还比例";
        data.ruleData.add(0,ruleBean);

        adapterRules.refresh(data.ruleData);
    }

}
